package com.zs.grain.ui

import android.app.Application
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import javax.inject.Inject
import com.zs.grain.service.*
import com.xnkj1688.semplibrary.mvvm.BaseViewModel
import com.zs.grain.tool.clockManage.LocalDataBase
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import java.text.SimpleDateFormat
import java.util.*
import kotlin.concurrent.schedule

class DailyViewModel @Inject constructor(
    val mApp: Application,
    val mDataService: DataService,
    val mDeviceService: DeviceService
) : BaseViewModel(mApp) {

    val terminalId = MutableLiveData<Int>()
    val errors = MutableLiveData<String>()
    val testResults = MutableLiveData<String>()
    val Logs = MutableLiveData<String>()
    val ListLogs = MutableLiveData<MutableList<LogItemViewModel>>()
    //val debugLogs = MutableLiveData<String>()

    private var mStateTimer: Timer? = null

    init {
        terminalId.value = mDataService.terminalId.toInt()
        val sdFormatter = SimpleDateFormat("MM-dd HH:mm")
        sdFormatter.timeZone = TimeZone.getTimeZone("GMT+8")
        var list=ArrayList<LogItemViewModel>()
        var Logs2 = mDataService.getSystemLogs(50)
        //var logBuilder = StringBuilder()
        for (log in Logs2) {
            var model = LogItemViewModel(mApp)
            //model.id = item.id.toInt()
            model.name = "     admin:" + sdFormatter.format(log.timestamp.time) + "    " +  log.content
            list.add(model)
        }
        ListLogs.value = list
    }

    var tempError = ""//
    fun startStateTimer() {
        this.mStateTimer?.cancel()
        this.mStateTimer = Timer()
        val sdFormatter = SimpleDateFormat("MM-dd HH:mm")
        sdFormatter.timeZone = TimeZone.getTimeZone("GMT+8")
        this.mStateTimer?.schedule(0, 500) {
            var list=ArrayList<LogItemViewModel>()
            var Logs2 = mDataService.getSystemLogs(50)
            var logBuilder = StringBuilder()
            for (log in Logs2) {
                var model = LogItemViewModel(mApp)
                model.name = "     admin:" + sdFormatter.format(log.timestamp.time) + log.content
                list.add(model)
            }
            if(mDeviceService.errorsString!=null && !mDeviceService.errorsString.isEmpty()) {
                if(tempError!=mDeviceService.errorsString){
                    tempError = mDeviceService.errorsString
                    //mDataService.insertLog("error:"+tempError)
                }
            }
           errors.postValue(mDeviceService.errorsString)
            //if(!errors.toString().isEmpty())   LocalDataBase.updateClockSettingEnable("reboot",false)
        }
    }

    fun stopStateTimer() {
        this.mStateTimer?.cancel()
        this.mStateTimer = null
    }

    fun openBigDoor() {
        //mDeviceService.mcuOpenDoor()
        mDataService.insertLog("Operation:开大门")
        mDeviceService.openBigDoor()
    }

    fun openSmallDoor() {
        mDataService.insertLog("Operation:开小门")
        mDeviceService.openSmallDoor()
    }

    fun fillWater() {
        mDataService.insertLog("Operation:填充水箱")
        Single.just(1)
                .subscribeOn(Schedulers.io())
                .subscribe({
                    this.uiNotify.postValue(this.getNotify("ShowProgress"))
                    mDeviceService.fillWater()
                    this.uiNotify.postValue(this.getNotify("HideProgress"))
                }, {
                    it.printStackTrace()
                })
    }

    fun dynTest() {
        this.showToast("暂不支持", Toast.LENGTH_LONG)
//        Single.just(1)
//                .subscribeOn(Schedulers.io())
//                .subscribe({
//                    this.uiNotify.postValue(this.getNotify("ShowProgress"))
//                    mDeviceService.dynTest()
//                    this.uiNotify.postValue(this.getNotify("HideProgress"))
//                }, {
//                    it.printStackTrace()
//                })
    }

    fun rushTest(){
        mDataService.insertLog("Operation:冲洗机器")
        Single.just(1)
            .subscribeOn(Schedulers.io())
            .subscribe({
                this.uiNotify.postValue(this.getNotify("ShowProgress"))
                mDeviceService.rushT()
                this.uiNotify.postValue(this.getNotify("HideProgress"))
            }, {
                it.printStackTrace()
            })
    }

    fun distinct(){
        mDataService.insertLog("Operation:消毒机器")
        Single.just(1)
            .subscribeOn(Schedulers.io())
            .subscribe({
                this.uiNotify.postValue(this.getNotify("ShowProgress"))
                mDeviceService.distinctT()
                this.uiNotify.postValue(this.getNotify("HideProgress"))
            }, {
                it.printStackTrace()
            })
    }

    fun fillCup() {
        mDataService.insertLog("Operation:填杯复位")
        Single.just(1)
                .subscribeOn(Schedulers.io())
                .subscribe({
                    this.uiNotify.postValue(this.getNotify("ShowProgress"))
                    mDeviceService.fillCup()
                    this.uiNotify.postValue(this.getNotify("HideProgress"))
                }, {
                    it.printStackTrace()
                })
    }

    fun onClearError() {
        mDataService.insertLog("Operation:手动清除错误")
        mDeviceService.clearAllError()
        //LocalDataBase.updateClockSettingEnable("reboot",true)
    }
}